package com.gtt.jr.util;

import java.io.IOException;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

/**
 * JSON转换工具
 * 
 * @author 高甜甜(gao12581@sina.com)
 * @date 2010-8-29
 * 
 */
public class JacksonUtils {

	private static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * 将对象转换为JSON格式
	 * 
	 * @param model
	 * @return
	 * @throws IOException
	 */
	public static String convertToJsonStr(Object model) throws IOException {
		return MAPPER.writeValueAsString(model);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> readFromStr(String content)
			throws IOException {
		return readFromStr(content, Map.class);
	}

	public static <T> T readFromStr(String content, Class<T> clazz)
			throws IOException {
		return MAPPER.readValue(content, clazz);
	}
}
